#include<pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include "Constant.h"


void constant_init(){
	char buffer[1000];
	FILE *read_fp;
	int chars_read;
	memset(buffer,0,1000);
	read_fp = popen("ifconfig eth1 | grep \"inet addr\"","r");
	if(read_fp!=NULL){
		chars_read=fread(buffer, sizeof(char),999,read_fp);
		pclose(read_fp);
        int i=0;
        for(i=0;i<chars_read;i++){
			if(buffer[i]==':')
			    break;
		}
		int j=0;
		for(i++,j=0;i<chars_read;i++){
			if(buffer[i]!=' '){
				HOSTIP[j++]=buffer[i];
			}
			else{
				break;
			}
		}

		for(;i<chars_read;i++){
			if(buffer[i]==':')
			    break;
		}
		for(i++,j=0;i<chars_read;i++){
			if(buffer[i]!=' '){
				BROADCASTIP[j++]=buffer[i];
			}
			else{
				break;
			}
		}


		HOSTID=0;
		int tmplen = strlen(HOSTIP);
		for(i=tmplen-1,j=1; i>=0; i--){
		    if(HOSTIP[i]=='.')
				break;
			else{
				HOSTID=HOSTID+(HOSTIP[i]-'0')*j;
			}
			j=j*10;
		}
	}
}
